/*
	Sensor.cpp
*/
#include "Queue.h"

template <class T>
Queue<T>::Queue()
{
	init(QUEUE_MAXSIZE);
	isLoop = false;
}

template <class T>
Queue<T>::Queue(int size)
{
	init(size);
	isLoop = false;
}
template <class T>
Queue<T>::Queue(int size, bool isloop)
{
	init(size);
	isLoop = isloop;
}

template <class T>
void Queue<T>::init(int size)
{
	maxSize = size;
	data = new T[size];
	index = -1;
}

template <class T>
bool Queue<T>::empty()
{
	return index == -1;
}

template <class T>
int Queue<T>::size()
{
	int len = index + 1;
	return len < maxSize ? len : maxSize;
}

template <class T>
bool Queue<T>::push(T e)
{
	if (!isLoop && (index + 1) >= maxSize){

		return false;
	}
	
	data[(++index) % maxSize] = e;
	return true;
}

template <class T>
bool Queue<T>::pop(T &e)
{
	if(index == -1) return false;

	e = data[index--];
	return true;
}

template <class T>
bool Queue<T>::get(T &e)
{
	if(index == -1) return false;
	e = data[index];
	return true;
}

template <class T>
bool Queue<T>::get(T &e, int offset)
{
	int len = size();
	if (len == -1) return false;

	e = data[((index + offset) % len + len) % len];
	return true;
}

template <class T>
bool Queue<T>::clear()
{
	index = -1;
	return true;
}